package com.aqa.concurrent;

import java.util.UUID;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

public class MultiThreadUpdateHashMap {

    private static int corePoolSize = Runtime.getRuntime().availableProcessors();

    private static ExecutorService executor = new ThreadPoolExecutor(10, 10, 10L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(Integer.MAX_VALUE), new ThreadPoolExecutor.CallerRunsPolicy());
    AtomicInteger count = new AtomicInteger();

    public static void main(String[] args) throws InterruptedException {

        ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>();
        IntStream.range(1, 100000000).forEach(i -> {
            executor.submit(() -> {
//                try {
//                    Thread.sleep(Math.abs(new Random(1000).nextInt()));
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                map.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
            });
        });

        System.out.println("=========================================");
        while (!executor.isTerminated()) {
            System.out.println(map.size());
        }
        Thread.currentThread().join();


    }


}
